/**
 * Desc
 * 
 * @description Holp You Do Good But Not Evil
 * @copyright Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author Chenyong Cai <ycchen@iwshop.cn>
 * @package Wshop
 * @link http://www.iwshop.cn
 */
var priceHashId = 0;
require(['config'], function (config) {
	require(['util', 'jquery', 'Spinner', 'Cart', 'Slider', 'Tiping'], function (util, $, Spinner, Cart, Slider, Tiping) {
		$('#list-loading').show();
		Cart.check();
		var cat = $('#cat').val();
		plus_click();
		cart_list_show();
		select_pro();
		empty_cart();
		prolist_scroll();
		fnLoadCatlist(cat);

		function select_pro() {
			$(".left-cat ul li").click(

			function () {
				var catId = $(this).find('.cat-btn').attr('data-catid');
				cat = catId;
				fnLoadCatlist(catId);
				$(".left-cat ul li.active").removeClass('active');
				$(this).addClass('active');
			})
		}

		function prolist_scroll() {
			var left_height = $(window).height();
			$(".left-cat").css('height', left_height + 'px');
			$(".right-pro").css('height', left_height + 'px');
		}

		function empty_cart() {
			$(".header-right").click(function () {
				var r = confirm("确认清空购物车？");
				if (r) {
					$.post(shoproot + '?/Cart/ajaxDelCart/', function (Res) {
						//$(".cart-list ul").remove();
						Cart.clear();
						$(".number").html(Cart.count());
						window.location.reload();
					});
				}
			});
		}

		function cart_list_show() {
			$(".cart-list img").click(function () {
				$(".cart-list").hide();
				fnLoadCatlist(cat);
			});
			$(".shopping-cart").click(

			function () {
				var count = Cart.count();
				if ($(".cart-list").hasClass('active')) {
					$(".cart-list").hide();
					$(".cart-list").removeClass('active');
					fnLoadCatlist(cat);
				} else if (count > 0) {
					$(".cart-list").show();
					$(".cart-list").addClass('active');
					$.post(shoproot + '?/Index/ajaxGetCartProducts/', clearResult);
				}
			})
		}

		function clearResult(data) {
			$('#cart_list').html(data);
			cart_proname_wd();
			cart_plus_click();
			cart_minus_click();
		}
		// $(".number").html(Cart.count());

		function cart_proname_wd() {
			var win_wd = $(window).width();
			var win_hg = $(window).height();
			$(".title-pro").css({
				'max-width': win_wd - 175
			});
			$(".cart-list ul").css({
				'max-height': win_hg / 2
			});
		}

		function plus_click() {
			$(".icon-plus").click(

			function () {
				var hash = $(this).parents('li').find(".pro-intru").attr('data-hash');
				var numNode = $(this).parents('li').find('.num');
				Cart.set(hash, parseInt(numNode.text()) + 1, doCallback);
				$(this).parents(".buy-num").find(".hidden").show();
				numNode.html(parseInt(numNode.text()) + 1);
			})
		}

		function minus_click() {
			$(".icon-minus").click(

			function () {
				var hash = $(this).parents('li').find(".pro-intru").attr('data-hash');
				var numNode = $(this).parents('li').find('.num');
				var p = $(this).parents('li').find(".pro-intru").attr('data-p');
				var sp = $(this).parents('li').find(".pro-intru").attr('data-sp');
				var num = parseInt(numNode.text()) - 1;
				if (num < 1) {
					num = 0;
					Cart.del(p, sp);
					$.post('?/Index/ajaxRemoveProductUPdateData/', {
						pid: p,
						sid: sp
					}, doCallback);
					$(this).parents(".buy-num").find(".hidden").hide();
				} else {
					Cart.set(hash, parseInt(numNode.text()) - 1, doCallback);
				}
				numNode.html(num);
			})
		}

		function doCallback(data) {
			if (data != "") {
				// alert(JSON.stringify(data));
				//val total = data.total;
				//alert(data.total);
				$(".cart-price ").html("&yen;" + data.total);
				if (data.topCats != "") {
					var cat = data.topCats;
					for (var i = 0; i < cat.length; i++) {
						$(".left-cat li").each(function () {
							var v = $(this).find('.cat-btn').attr('data-catid');
							if (cat[i].cat_id == v) {
								if (typeof(cat[i].count) != 'undefined') {
									$(this).find('.cat-num').html(cat[i].count);
									$(this).find(".cat-num").show();
								} else {
									$(this).find('.cat-num').hide();
								}
							}
						});
					}
				}
				$(".number").html(Cart.count());
			}
		}
		// 购买
		$('#buy').click(function () {
			if (Cart.count() == 0) {
				alert('购物车空空的');
				return;
			}
			$.post(shoproot + "?/Cart/add_product_to_cart", {
				cartData: window.localStorage.getItem('cart'),
			}, function result(data) {
				window.location.href = shoproot + "?/Cart/index_order";
			});
		});

		function cart_plus_click() {
			$('.prolist #cart_plus').click(function () {
				var p = $(this).parents('li').find(".pro-buy-num").attr('data-p');
				var hash = $(this).parents('li').find(".pro-buy-num").attr('data-hash');
				var numNode = $(this).parents('li').find('.num');
				$(".right-pro ul li").each(function (i) {
					var pro_p=$(".right-pro ul li").eq(i).find(".pro-intru").attr('data-p');
					var plus_num=$(".right-pro ul li").eq(i).find(".num");
					if(pro_p==p){
						plus_num.html(parseInt(plus_num.text())+1);
						}
				});
				Cart.set(hash, parseInt(numNode.text()) + 1, doCallback);
				$(this).parents(".buy-num").find(".hidden").show();
				numNode.html(parseInt(numNode.text()) + 1);
			});
		}

		function cart_minus_click() {
			$('.prolist #cart_minus').click(function () {
				var count = Cart.count();
				var hash = $(this).parents('li').find(".pro-buy-num").attr('data-hash');
				var numNode = $(this).parents('li').find('.num');
				var p = $(this).parents('li').find(".pro-buy-num").attr('data-p');
				var sp = $(this).parents('li').find(".pro-buy-num").attr('data-sp');
				$(".right-pro ul li").each(function (i) {
					var pro_p=$(".right-pro ul li").eq(i).find(".pro-intru").attr('data-p');
					var minus_num=$(".right-pro ul li").eq(i).find(".num");
					if(pro_p==p){
						minus_num.html(parseInt(minus_num.text())-1);
						if(parseInt(minus_num.text())==0){
							minus_num.parents(".buy-num").find(".hidden").hide();
							}
						}
					
				});
				var num = parseInt(numNode.text()) - 1;
				if (num < 1) {
					num = 0;
					Cart.del(p, sp);
					$(this).parents('li').remove();
					$.post('?/Index/ajaxRemoveProductUPdateData/', {
						pid: p,
						sid: sp
					}, doCallback);
				} else {
					Cart.set(hash, parseInt(numNode.text()) - 1, doCallback);
				}
				numNode.html(num);
				if (count < 2) {
					$(".cart-list").hide();
				}
			});
		}

		function touch_left_right() {
			$(".left-cat").on('touchstart', function () {
				$(".left-cat").css('position', 'absolute');
				$(".right-pro").css('position', 'fixed');
			});
			$(".right-pro").on('touchstart', function () {
				$(".right-pro").css('position', 'absolute');
				$(".left-cat").css('position', 'fixed');
			});
		}

		function fnLoadCatlist(cat) {
			$('#rightContainer').load('?/Index/ajax_list_item/id=' + cat, function () {
				plus_click();
				minus_click();
				touch_left_right();
			});
		}
	});
});